package com.front.teacher.teacherapp.view.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.front.teacher.teacherapp.R;
import com.front.teacher.teacherapp.base.BaseActivity;
import com.front.teacher.teacherapp.utils.SharedPreferencesHelper;
import com.front.teacher.teacherapp.view.adapter.WelcomePagerAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.InjectView;

public class WelcomeActivity extends BaseActivity {
    private RadioButton[] radioButtons;
    private Context mContext = this;
    private int[] imageViewId = {R.mipmap.welcome1, R.mipmap.welcome2, R.mipmap.welcome3, R.mipmap.welcome4};
    public static final String FIRST_TOKE = "FIRST";
    private Button buttonWelcome;
    @InjectView(R.id.radiogroup_welcome)
    RadioGroup radiogroupWelcome;
    private WelcomePagerAdapter adapter;
    private List<ImageView> imageViews = new ArrayList<>();
    @InjectView(R.id.welcome_viewpager)
    ViewPager welcomeViewpager;

    @Override
    public void initView() {
        buttonWelcome = (Button) findViewById(R.id.button_start_welcome);
        buttonWelcome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, FirstActivity.class);
                startActivity(intent);
                finish();
            }
        });
        int firstTake = SharedPreferencesHelper.getInstance(mContext).getIntValue(FIRST_TOKE);
        if (firstTake == 1) {
            Intent intent = new Intent(mContext, FirstActivity.class);
            startActivity(intent);
            finish();
        } else {
            getInitView();
        }
    }

    /**
     * 加载本页面的布局文件
     */
    private void getInitView() {

        radioButtons = new RadioButton[4];
        for (int i = 0; i < 4; i++) {
            setImageView(imageViewId[i]);
            radioButtons[i] = new RadioButton(this);
            radioButtons[i].setButtonDrawable(R.drawable.dot_selector);
            radioButtons[i].setTag(i);
            radioButtons[i].setPadding(20, 40, 20, 20);
            radiogroupWelcome.addView(radioButtons[i]);
            final int finalI = i;
            radioButtons[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    welcomeViewpager.setCurrentItem(finalI);
                }
            });
        }
        adapter = new WelcomePagerAdapter(imageViews);
        welcomeViewpager.setAdapter(adapter);
        welcomeViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                radioButtons[position].setChecked(true);
            }

            @Override
            public void onPageSelected(int position) {
                //设置立即使用
                if (position == imageViews.size() - 1) {
                    buttonWelcome.setVisibility(View.VISIBLE);
                } else {
                    buttonWelcome.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //给sharedPreferences中添加标记
        SharedPreferencesHelper.getInstance(mContext).putIntValue(FIRST_TOKE, 1);
    }

    private void setImageView(int imageId) {
        ImageView imageView = new ImageView(this);
        imageView.setImageResource(imageId);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageViews.add(imageView);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_welcome;
    }

    @Override
    public void initData() {
        super.initData();
    }

}
